﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Touch2Like.DAL;
using Touch2Like.Model;
using System.Data;
using System.Collections;
using OpenFlashChart;
using System.Xml;
using System.Web.UI.HtmlControls;

namespace Touch2Like.Web.Customer.Manage
{
    public partial class Play : System.Web.UI.Page
    {
        OpenFlashChart.OpenFlashChart chart = new OpenFlashChart.OpenFlashChart();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (WebUtility.UserGUID == null || WebUtility.UserGUID == "0")
            {
                Response.Clear();
                Response.Write("<script>parent.ResetURL('/Customer/signin.aspx?next=" + Server.UrlEncode("/Customer/Index.aspx") + "');</script>");
                Response.End();
            }

            if (!IsPostBack)
            {
                tb_EndDate.Text = DateTime.Now.ToShortDateString();
                tb_StartDate.Text = DateTime.Today.AddMonths(-1).ToShortDateString();
                runStatistics();
            }
        }

        protected void btn_Refresh_Click(object sender, EventArgs e)
        {
            runStatistics();
        }

        void runStatistics()
        {
            int count = 0;
            ActivityDAL tag = new ActivityDAL();
            DateTime startDate = Convert.ToDateTime(tb_StartDate.Text), endDate = Convert.ToDateTime(tb_EndDate.Text).AddDays(1);
            DataTable dt = tag.GetDateStatistics(WebUtility.UserGUID, "", startDate, endDate, "Touch2Play");
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    count += Convert.ToInt32(dt.Rows[i]["TouchCount"].ToString());
                }
                lbl_TotalTouch.Text = "TOTAL: " + count;
            }
            ArrayList data1 = new ArrayList();
            List<string> label1 = new List<string>();
            double max = 0;
            for (int i1 = 0; startDate < endDate; startDate = startDate.AddDays(1))
            {
                if (i1 % 5 == 0)
                    label1.Add(FormatShortDate(startDate));
                else
                    label1.Add("");

                double val = 0;
                ArrayList list = new ArrayList();
                if (dt.Select("TouchDay='" + FormatDate(startDate) + "'").Length > 0)
                {
                    val = Convert.ToDouble(dt.Select("TouchDay='" + FormatDate(startDate) + "'")[0]["TouchCount"]);
                    //LineDotValue v = new LineDotValue(val);
                    /*v.Tip = FormatDate(startDate);*/
                    data1.Add(val);
                    if (val > max) max = val;
                }
                else
                {
                    data1.Add(0);
                }
                i1 = i1 + 1;
            }

            OpenFlashChart.Line line1 = new Line();
            line1.Values = data1;
            line1.DotStyleType.DotSize = 3;
            line1.Width = 3;

            line1.Colour = "#54AEE2";

            line1.Tooltip = "#val#";

            chart.X_Axis.Labels.SetLabels(label1);

            chart.Bgcolor = "#ffffff";
            chart.X_Axis.GridColour = chart.Bgcolor;
            chart.X_Axis.Colour = "#DADADA";
            chart.X_Axis.Offset = false;
            chart.Y_Axis.GridColour = "#F0F0F0";
            chart.Y_Axis.Colour = chart.Bgcolor;//"#DADADA";
            chart.Y_Axis.Offset = false;
            chart.X_Axis.Steps = 5;
            chart.Y_Axis.Steps = 3;
            chart.AddElement(line1);

            chart.Title = new Title("");
            chart.Title.Style = "{display:none;}";
            int temp1 = Convert.ToInt32(max / 3) + 1;
            chart.Y_Axis.SetRange(0, temp1 * 3, temp1);
            chart.Tooltip = new ToolTip("Global tooltip:#val#");
            chart.Tooltip.Shadow = false;
            chart.Tooltip.Stroke = 1;
            chart.Tooltip.Colour = "#236DC0";
            chart.Tooltip.BodyStyle = "{color:#ffffff;}";
            chart.Tooltip.BackgroundColor = "#3192FF";
            chart.Tooltip.TitleStyle = "{color:#ffffff;}";
            chart.Tooltip.MouseStyle = ToolTipStyle.CLOSEST;
            OpenFlashChartControl1.EnableCache = false;
            OpenFlashChartControl1.Chart = chart;
        }

        string FormatDate(DateTime d)
        {
            string year = d.Year.ToString(), month = d.Month.ToString(), day = d.Day.ToString();
            if (d.Month.ToString().Length < 2) month = "0" + d.Month;
            if (d.Day.ToString().Length < 2) day = "0" + d.Day;
            return year + "-" + month + "-" + day;
        }

        string FormatShortDate(DateTime d)
        {
            string year = d.Year.ToString(), month = d.Month.ToString(), day = d.Day.ToString();
            return month + "/" + day;
        }
    }
}